package com.ost.kautilya.utils.metrics;

import java.util.ArrayList;
import java.util.List;

import com.ost.kautilya.utils.Metric.DataType;

public class StandardDeviation extends Mean {
	private static final long serialVersionUID = 1L;
	private final List<Double> population;

	public StandardDeviation(DataType dt) {
		super(dt);
		population = new ArrayList<Double>();
	}

	@Override
	public void push(long index, Number number) {
		super.push(index, number);
		population.add(number.doubleValue());
	}

	@Override
	protected double asDouble() {
		if (population.size() <= 1) return 0;
		double avg = super.asDouble();
		double sna = 0;
		for (double ni : population)
			sna += Math.pow(ni - avg, 2);
		return Math.sqrt(sna / population.size() - 1);
	}

	public double getMean() {
		return super.asDouble();
	}
	
	@Override
	public void reset() {
		super.reset();
		population.clear();
	}
}
